package handler

import (

	"golang.org/x/net/context"
	role "git.oschina.net/fanbuchi/xgggh/oauth2/proto/role"
	"git.oschina.net/fanbuchi/xgggh/oauth2/model"
	"git.oschina.net/fanbuchi/xgggh/oauth2/db"
	"encoding/json"
)

type Role struct{}



//micro query com.xgggh.srv.oauth2 RoleSrv.Create '{"name":"users","description":"normal users"}'
//micro query com.xgggh.srv.oauth2 RoleSrv.Create '{"name":"dev","description":"developer"}'
//micro query com.xgggh.srv.oauth2 RoleSrv.Create '{"name":"admin","description":"admin"}'
//micro query com.xgggh.srv.oauth2 RoleSrv.Create '{"name":"superusers","description":"super users"}'
//micro query com.xgggh.srv.oauth2 RoleSrv.Create '{"name":"customerservice","description":"Customer Service"}'


// Call is a single request handler called via client.Call or the generated client code
func (e *Role) Create(ctx context.Context, req *role.RoleCreateReq, rsp *role.RoleCreateRsp) error {
	//password change
	role:=model.Role{Name:req.Name,
		Description:req.Description,
	}
	err:=db.GetDB().Create(&role).Error

	return err
}
//micro query com.xgggh.srv.oauth2 RoleSrv.Query `{"id":245493286254346241}`
//micro call com.xgggh.srv.oauth2 Role.Query {\"id\":359114213878562817}
func (e *Role) Query(ctx context.Context, req *role.RoleQueryReq, rsp *role.RoleQueryRsp) error {
	var _role model.Role
	err:=db.GetDB().First(&_role, req.Id).Error
	if _role.ID<=0 {
		return nil
	}

	var deleted int64
	if _role.DeletedAt!=nil {
		deleted=_role.DeletedAt.Unix()
	}
	rsp.Ok=true
	rsp.Data=&role.RoleMsg{Id:int64(_role.ID),
		Name:_role.Name,
		Description:_role.Description,
		CreatedAt:_role.CreatedAt.Unix(),
		UpdatedAt:_role.UpdatedAt.Unix(),
		DeletedAt:deleted,
		}
	return err
}

//micro query com.xgggh.srv.oauth2 RoleSrv.Update '{"id":245493286254346241,"role":{"name":"developer","description":"developer"}}'

func (e *Role) Update(ctx context.Context, req *role.RoleUpdateReq, rsp *role.RoleUpdateRsp) error {
	_role:=req.Role
	if  _role==nil{
		return nil
	}
	_role.Id=req.Id
	err:= db.GetDB().Model(&_role).Updates(model.Role{
		Name:_role.Name,
		Description:_role.Description}).Error
	if err ==nil{
		rsp.Data=_role
	}
	return err
}
//micro query com.xgggh.srv.oauth2 RoleSrv.Delete '{"id":245064531848396801}'

func (e *Role) Delete(ctx context.Context, req *role.RoleDeleteReq, rsp *role.RoleDeleteRsp) error {
	_role:=model.Role{}
	_role.ID=uint(req.Id)
	err:=db.GetDB().Delete(&_role).Error
	return err
}

//micro query com.xgggh.srv.oauth2 RoleSrv.Search '{"page":1,"per":10}'
//search by name,desc ...
//micro query com.xgggh.srv.oauth2 RoleSrv.Search '{"page":1,"per":10,"params":{"name":"dev"}}'

func (e *Role) Search(ctx context.Context, req *role.RoleSearchReq, rsp *role.RoleSearchRsp) error {
	var _roles [] *model.Role;
	var __role [] *role.RoleMsg;
	//page
	var page int64=1;
	if req.Page>0 {
		page=req.Page
	}
	offset:=(page-1)*req.PerPage

	//support below data filter
	 params:=model.Role{
		 Name: req.Params["name"],
		 Description:req.Params["description"],
	 }
	//db
	err:=db.GetDB().
		Where(params).
		Offset(offset).
		Limit(req.PerPage).
		Find(&_roles).
		Error
	//data
	data,err:=json.Marshal(_roles)
	json.Unmarshal(data,&__role)
	if err ==nil{
		rsp.Data=__role
	}
	return  err
}